package com.jwdf.uda.ui.adapter.viewholder;

import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.ExtraBaseQuestion;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ItemReplyImageBinding;
import com.jwdf.uda.ui.activity.ReplyDetaiActivtiy;
import com.jwdf.uda.ui.activity.UserInfoActivity;
import com.jwdf.uda.ui.adapter.AbsViewHolder;
import com.jwdf.uda.ui.adapter.item.QuizDetailItem;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.view.GridImageLayout;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/7.
 */
public class QuizReplyImageViewHolder extends AbsViewHolder<QuizDetailItem> {
    private static final int ONE_IMAGE = 1;
    private static final int TWO_IMAGE = 2;
    private static final int THREE_IMAGE = 3;
    private static final int MAX_QUICK_IMAGE_COUNT = 3;
    private ItemReplyImageBinding mItemBinding;

    public QuizReplyImageViewHolder(View convertView) {
        super(convertView);
        mItemBinding = DataBindingUtil.bind(convertView);
        convertView.setOnClickListener(this);
        mItemBinding.topLayout.llHeadLayout.setOnClickListener(this);
    }

    @Override
    protected void onBindView(QuizDetailItem item) {
        PbBean.PbBaseQuestionAnswer reply = item.reply;
        if (reply == null) return;
        QuizReplyTxtViewHolder.onBindCommonView(mItemBinding.topLayout, mItemBinding.bottomLayout, reply);
        final List<PbBean.PbImage> images = reply.getQuickImageInfoList();
//        mItemBinding.tvImageCount.setText(images.size() + "张");
        final ArrayList<String> urls = new ArrayList<>();
        for (int i = 0; i < images.size(); i++) {
            PbBean.PbImage image = images.get(i);
            if (i < MAX_QUICK_IMAGE_COUNT) {
                urls.add(image.getUrl());
            }
        }
        mItemBinding.gridimageLayout.setImage(urls);
        if (reply.getQuickImageCount() > MAX_QUICK_IMAGE_COUNT) {
            mItemBinding.gridimageLayout.setThumbNum(reply.getQuickImageCount());
        }else{
            mItemBinding.gridimageLayout.setThumbNumGone();
        }
        mItemBinding.gridimageLayout.setOnImageItemClickListener(new GridImageLayout.OnImageItemClickListener() {
            @Override
            public void onImageClick(int index, ImageView imageView) {
//                PhotoActivity.start(imageView.getContext(), urls, index);
                toReplyDetail(mItemBinding.gridimageLayout);
            }
        });
//        int imageCount = images.size();
//        if (imageCount > 3) {
//            ImageLoader.getInstance().displayImage(images.get(0).getUrl(), mItemBinding.img0);
//            ImageLoader.getInstance().displayImage(images.get(1).getUrl(), mItemBinding.img1);
//            ImageLoader.getInstance().displayImage(images.get(2).getUrl(), mItemBinding.img2);
//            mItemBinding.img0.setVisibility(View.VISIBLE);
//            mItemBinding.img1.setVisibility(View.VISIBLE);
//            mItemBinding.img2.setVisibility(View.VISIBLE);
//        } else {
//            switch (imageCount) {
//                case ONE_IMAGE:
//                    ImageLoader.getInstance().displayImage(images.get(0).getUrl(), mItemBinding.img0);
//                    mItemBinding.img0.setVisibility(View.VISIBLE);
//                    break;
//                case TWO_IMAGE:
//                    ImageLoader.getInstance().displayImage(images.get(0).getUrl(), mItemBinding.img0);
//                    ImageLoader.getInstance().displayImage(images.get(1).getUrl(), mItemBinding.img1);
//                    mItemBinding.img0.setVisibility(View.VISIBLE);
//                    mItemBinding.img1.setVisibility(View.VISIBLE);
//                    break;
//                case THREE_IMAGE:
//                    ImageLoader.getInstance().displayImage(images.get(0).getUrl(), mItemBinding.img0);
//                    ImageLoader.getInstance().displayImage(images.get(1).getUrl(), mItemBinding.img1);
//                    ImageLoader.getInstance().displayImage(images.get(2).getUrl(), mItemBinding.img2);
//                    mItemBinding.img0.setVisibility(View.VISIBLE);
//                    mItemBinding.img1.setVisibility(View.VISIBLE);
//                    mItemBinding.img2.setVisibility(View.VISIBLE);
//                    break;
//            }
//        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ll_head_layout) {
            UserInfoActivity.start(v.getContext(), item.reply.getUserInfo().getUserId(), item.reply.getUserInfo().getIsDesigner());
            return;
        }
        toReplyDetail(v);
    }

    private void toReplyDetail(View v) {
        boolean isAuthor = TextUtils.equals(item.reply.getUserInfo().getUserId(), ConfigUtils.getUserId(v.getContext()));
        ReplyDetaiActivtiy.start(v.getContext(), item.reply.getAnswerId(), isAuthor, item.planVoteCount, new ExtraBaseQuestion());
    }
}
